#include "../include/deng.h"

// 普通用户的注册
void zhuce()
{
    FILE *fp = file_open("花名册.txt", "a");
    Node *z = (Node *)malloc(sizeof(Node));
    strcpy(z->data.userType, "user");
    z->data.value = 0;
    z->data.vip = 10;
    Link_list *L = nacun(); // 把文件里面的东西放在链表里面
    
    printf("请输入你的用户名：\n");
    while (1)
    {
        int flag_1 = 0;
        scanf("%s", z->data.name);
        Node *ptr = L->next;
        for (size_t i = 0; ptr != NULL; i++) // 循环比较
        {
            if (strcmp(ptr->data.name, z->data.name) == 0)
            {
                flag_1 = -98;
                break;
            }
            ptr = ptr->next; // 往后移
        }
        if (flag_1 == -98)
        {
            printf("该用户名已存在，请重新输入...\n");
            continue;
        }
        else
        {
            break;
        }
    }
    
    printf("请输入你的账号：\n");
    while (1)
    {
        int flag_1 = 0;
        scanf("%s", z->data.id);
        Node *ptr = L->next;
        for (size_t i = 0; ptr != NULL; i++) // 循环比较
        {
            if (strcmp(ptr->data.id, z->data.id) == 0)
            {
                flag_1 = -98;
                break;
            }
            ptr = ptr->next; // 往后移
        }
        if (flag_1 == -98)
        {
            printf("该账号已存在，请重新输入...\n");
            continue;
        }
        else
        {
            break;
        }
    }
    printf("请输入你的密码：\n");
    scanf("%s", z->data.pos);
    fprintf(fp, "%s;%s;%s;%s;%d;%d\n",
            z->data.userType, z->data.id, z->data.pos, z->data.name, z->data.value, z->data.vip);

    printf("注册成功，以下为你的信息\n");
    print_link_list_1(z);
    pause_and_continue();
    file_close(&fp);
    free_link_list(&L);
}